package Manage;

import java.io.File;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
 



import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
 
 
public class XMlParser {
	private HashMap<String, HashMap<String, String>> compoentFuncDict = new HashMap<String, HashMap<String, String>>();
	public HashMap<String, HashMap<String, String>> getCompoentFuncDict() {
		return compoentFuncDict;
	}

	private static XMlParser m_instance = null;
	private XMlParser() {
		
	}

	public static XMlParser getInstance() {
		if (m_instance == null) {
			m_instance = new XMlParser();
		}
		return m_instance;
	}
	
    public void loadXmlFile(String level) throws Exception {
    	
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        
        String userDir = System.getProperty("user.dir");
		// set the sourcePath
		String sourceFileStr = userDir + "\\..\\" + "Case" + "\\"
				+ "func_" + level + ".xml";
        File f = new File(sourceFileStr);
        Document doc = builder.parse(f);
        Element root = doc.getDocumentElement();
        //According to root node
        NodeList list = root.getElementsByTagName("Component");
        for (int i = 0; i < list.getLength(); i++) {
        	HashMap<String, String> funcDict = new HashMap<String, String>();
            //parse name of component
        	String componentName = "error";
            Element component = (Element) list.item(i);
            NamedNodeMap node = component.getAttributes();
            for (int x = 0; x < node.getLength(); x++) {
                Node attrItem = node.item(x);
                if (attrItem.getNodeName().equals("name"))
                {
                	componentName = attrItem.getNodeValue();
                	break;
                }
            }
            if (null != component.getElementsByTagName("ConstructFunction").item(0))
            	funcDict.put("ConstructFunction", component.getElementsByTagName("ConstructFunction").item(0).getTextContent());
            if (null != component.getElementsByTagName("InitFunction").item(0))
            	funcDict.put("InitFunction", component.getElementsByTagName("InitFunction").item(0).getTextContent());
            if (null != component.getElementsByTagName("ExternalTransitionFunction").item(0))
            	funcDict.put("ExternalTransitionFunction", component.getElementsByTagName("ExternalTransitionFunction").item(0).getTextContent());
            if (null != component.getElementsByTagName("InternalTransitionFunction").item(0))
            	funcDict.put("InternalTransitionFunction", component.getElementsByTagName("InternalTransitionFunction").item(0).getTextContent());
            if (null != component.getElementsByTagName("OutputFunction").item(0))
            	funcDict.put("OutputFunction", component.getElementsByTagName("OutputFunction").item(0).getTextContent());
            compoentFuncDict.put(componentName, funcDict);
            
        }
        System.out.println("XML parse succeeds!");
    }
 
}